<?php


namespace app\index\controller;


use think\Controller;
use think\Db;

class Index extends Controller
{


    public function uploadImages()
    {
        $menu = Db::name('i_menu_children')
            ->where('his is null')
            ->select();

        foreach($menu as $k => $v){
            $url = explode('/',$v['image']);
            $suffix = $url[count($url) - 1 ];
            $path = 'D:\phpstudy_pro\WWW\backstage\public\static\ai\image\image/'.$suffix;

            $imageData = file_get_contents($v['image']); // 获取图片数据

            if ($imageData !== false) {
                $filePath = $path;
                file_put_contents($filePath, $imageData); // 将图片数据写入文件
                Db::name('i_menu_children')
                    ->where(['id'=>$v['id']])
                    ->update(['his'=>$suffix]);
            } else {
                echo "无法下载图片";
            }


        }

        echo '结束';
    }

    public function getUrl()
    {
        $urls = DB::name('i_menu_children')
              ->alias('a')
             ->join('i_url b','a.name = b.name')
             ->field('a.*,b.url')
             ->select();

        foreach($urls as $k => $v){
            DB::name('i_menu_children')
                ->where('id',$v['id'])
                ->update(['url'=>$v['url']]);
        }

        echo '结束';
    }

    public function detail()
    {
        $id = input('id');
        $res = DB::name('i_menu_children')
             ->where('id',$id)
             ->find();
        echo '<script>window.location.href="'.$res['url'].'"</script>';die;
    }

    public function getVideo()
    {

        $file = file_get_contents('http://api.yujn.cn/api/zzxjj.php?type=video');
        echo '<video src="'+$file+'"></video>';die;
    }


    public function index()
    {

        $menu = Db::name('i_menu')
            ->where('status = 0')
            ->select();
        $this->assign('menu',$menu);

        $menuChildren = DB::name('i_menu_children')
                      ->alias('a')
                      ->join('i_menu b','a.pid = b.id')
                      ->field('a.*,b.name as pname,b.icon')
                      ->order('a.pid asc')
                      ->select();
        $icon = [];
        $childrenMenu = [];
        foreach($menuChildren as $k => $v){
            $childrenMenu[$v['pname']][] = $v;
            $icon[$v['pname']] = $v['icon'];
        }

        $this->assign('childrenMenu',$childrenMenu);
        $this->assign('icon',$icon);
        return view();
    }

    /**
     * use   获取测试数据
     * user  Hugh
     * date  2021/6/25 13:58
     */
    public function getData()
    {

        sleep(5);
        return success($data);
    }
}